package net.ssta.common;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlUtil {

	
	public static InputStream getUrlInputStream(String url) throws IOException{
		String cookie = "";
		int i=0;
	    do {
	        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
	        if(cookie.length() != 0)
	            conn.setRequestProperty("Cookie", cookie);
	        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
	        conn.setInstanceFollowRedirects(false);
	        int code = conn.getResponseCode();
	        if(code == HttpURLConnection.HTTP_MOVED_TEMP) {
	            cookie += conn.getHeaderField("Set-Cookie") + ";";
	        }
	        if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
	     	     return  conn.getInputStream();
	        }
	        if(i>2){
	        	return null;
	        }
	        i++;
	    } while(true);
	}
	
}
